Added support for class selectors and class attribute selectors on XML documents. Fixes jQuery bug #4167.

This commit is contained in:
John Resig 2009-02-16 18:23:59 +00:00
parent 8533da939d
commit 048fc4555f
5 changed files with 22 additions and 12 deletions

View file

@ -165,7 +165,8 @@ Sizzle.find = function(expr, context, isXML){
}; };
Sizzle.filter = function(expr, set, inplace, not){ Sizzle.filter = function(expr, set, inplace, not){
var old = expr, result = [], curLoop = set, match, anyFound; var old = expr, result = [], curLoop = set, match, anyFound,
isXMLFilter = set && set[0] && isXML(set[0]);
while ( expr && set.length ) { while ( expr && set.length ) {
for ( var type in Expr.filter ) { for ( var type in Expr.filter ) {
@ -178,7 +179,7 @@ Sizzle.filter = function(expr, set, inplace, not){
} }
if ( Expr.preFilter[ type ] ) { if ( Expr.preFilter[ type ] ) {
match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not ); match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
if ( !match ) { if ( !match ) {
anyFound = found = true; anyFound = found = true;
@ -357,9 +358,13 @@ var Expr = Sizzle.selectors = {
} }
}, },
preFilter: { preFilter: {
CLASS: function(match, curLoop, inplace, result, not){ CLASS: function(match, curLoop, inplace, result, not, isXML){
match = " " + match[1].replace(/\\/g, "") + " "; match = " " + match[1].replace(/\\/g, "") + " ";
if ( isXML ) {
return match;
}
for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) { for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
if ( elem ) { if ( elem ) {
if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) { if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
@ -397,10 +402,10 @@ var Expr = Sizzle.selectors = {
return match; return match;
}, },
ATTR: function(match){ ATTR: function(match, curLoop, inplace, result, not, isXML){
var name = match[1].replace(/\\/g, ""); var name = match[1].replace(/\\/g, "");
if ( Expr.attrMap[name] ) { if ( !isXML && Expr.attrMap[name] ) {
match[1] = Expr.attrMap[name]; match[1] = Expr.attrMap[name];
} }
@ -588,7 +593,8 @@ var Expr = Sizzle.selectors = {
return (match === "*" && elem.nodeType === 1) || elem.nodeName === match; return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
}, },
CLASS: function(elem, match){ CLASS: function(elem, match){
return match.test( elem.className ); return (" " + (elem.className || elem.getAttribute("class")) + " ")
.indexOf( match ) > -1;
}, },
ATTR: function(elem, match){ ATTR: function(elem, match){
var name = match[1], var name = match[1],
@ -815,8 +821,10 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl
return; return;
Expr.order.splice(1, 0, "CLASS"); Expr.order.splice(1, 0, "CLASS");
Expr.find.CLASS = function(match, context) { Expr.find.CLASS = function(match, context, isXML) {
return context.getElementsByClassName(match[1]); if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
return context.getElementsByClassName(match[1]);
}
}; };
})(); })();

View file

@ -1,3 +1,3 @@
foobar = "bar"; var foobar = "bar";
jQuery('#ap').html('bar'); jQuery('#ap').html('bar');
ok( true, "test.js executed"); ok( true, "test.js executed");

View file

@ -1,5 +1,5 @@
<script type="text/javascript"> <script type="text/javascript">
testFoo = "foo"; var testFoo = "foo";
jQuery('#foo').html('foo'); jQuery('#foo').html('foo');
ok( true, "test2.html executed" ); ok( true, "test2.html executed" );
</script> </script>

View file

@ -6,7 +6,7 @@
<jsconf xmlns="http://www.example.com/ns1"> <jsconf xmlns="http://www.example.com/ns1">
<response xmlns:ab="http://www.example.com/ns2"> <response xmlns:ab="http://www.example.com/ns2">
<meta> <meta>
<component id="seite1"> <component id="seite1" class="component">
<properties xmlns:cd="http://www.example.com/ns3"> <properties xmlns:cd="http://www.example.com/ns3">
<property name="prop1"> <property name="prop1">
<thing /> <thing />

View file

@ -35,10 +35,12 @@ test("element", function() {
if ( location.protocol != "file:" ) { if ( location.protocol != "file:" ) {
test("XML Document Selectors", function() { test("XML Document Selectors", function() {
expect(5); expect(7);
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(".component", xml).length, 1, "Class selector" );
equals( jQuery("[class*=component]", xml).length, 1, "Attribute selector for class" );
equals( jQuery("property[name=prop2]", xml).length, 1, "Attribute selector with name" ); 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("[name=prop2]", xml).length, 1, "Attribute selector with name" );
equals( jQuery("#seite1", xml).length, 1, "Attribute selector with ID" ); equals( jQuery("#seite1", xml).length, 1, "Attribute selector with ID" );