Skip to content

Commit 048fc45

Browse files
committed
Added support for class selectors and class attribute selectors on XML documents. Fixes jQuery bug jquery#4167.
1 parent 8533da9 commit 048fc45

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

src/selector.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ Sizzle.find = function(expr, context, isXML){
165165
};
166166

167167
Sizzle.filter = function(expr, set, inplace, not){
168-
var old = expr, result = [], curLoop = set, match, anyFound;
168+
var old = expr, result = [], curLoop = set, match, anyFound,
169+
isXMLFilter = set && set[0] && isXML(set[0]);
169170

170171
while ( expr && set.length ) {
171172
for ( var type in Expr.filter ) {
@@ -178,7 +179,7 @@ Sizzle.filter = function(expr, set, inplace, not){
178179
}
179180

180181
if ( Expr.preFilter[ type ] ) {
181-
match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not );
182+
match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter );
182183

183184
if ( !match ) {
184185
anyFound = found = true;
@@ -357,9 +358,13 @@ var Expr = Sizzle.selectors = {
357358
}
358359
},
359360
preFilter: {
360-
CLASS: function(match, curLoop, inplace, result, not){
361+
CLASS: function(match, curLoop, inplace, result, not, isXML){
361362
match = " " + match[1].replace(/\\/g, "") + " ";
362363

364+
if ( isXML ) {
365+
return match;
366+
}
367+
363368
for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
364369
if ( elem ) {
365370
if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
@@ -397,10 +402,10 @@ var Expr = Sizzle.selectors = {
397402

398403
return match;
399404
},
400-
ATTR: function(match){
405+
ATTR: function(match, curLoop, inplace, result, not, isXML){
401406
var name = match[1].replace(/\\/g, "");
402407

403-
if ( Expr.attrMap[name] ) {
408+
if ( !isXML && Expr.attrMap[name] ) {
404409
match[1] = Expr.attrMap[name];
405410
}
406411

@@ -588,7 +593,8 @@ var Expr = Sizzle.selectors = {
588593
return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
589594
},
590595
CLASS: function(elem, match){
591-
return match.test( elem.className );
596+
return (" " + (elem.className || elem.getAttribute("class")) + " ")
597+
.indexOf( match ) > -1;
592598
},
593599
ATTR: function(elem, match){
594600
var name = match[1],
@@ -815,8 +821,10 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl
815821
return;
816822

817823
Expr.order.splice(1, 0, "CLASS");
818-
Expr.find.CLASS = function(match, context) {
819-
return context.getElementsByClassName(match[1]);
824+
Expr.find.CLASS = function(match, context, isXML) {
825+
if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
826+
return context.getElementsByClassName(match[1]);
827+
}
820828
};
821829
})();
822830

test/data/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
foobar = "bar";
1+
var foobar = "bar";
22
jQuery('#ap').html('bar');
33
ok( true, "test.js executed");

test/data/test2.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script type="text/javascript">
2-
testFoo = "foo";
2+
var testFoo = "foo";
33
jQuery('#foo').html('foo');
44
ok( true, "test2.html executed" );
55
</script>

test/data/with_fries.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<jsconf xmlns="http://www.example.com/ns1">
77
<response xmlns:ab="http://www.example.com/ns2">
88
<meta>
9-
<component id="seite1">
9+
<component id="seite1" class="component">
1010
<properties xmlns:cd="http://www.example.com/ns3">
1111
<property name="prop1">
1212
<thing />

test/unit/selector.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ test("element", function() {
3535

3636
if ( location.protocol != "file:" ) {
3737
test("XML Document Selectors", function() {
38-
expect(5);
38+
expect(7);
3939
stop();
4040
jQuery.get("data/with_fries.xml", function(xml) {
4141
equals( jQuery("foo_bar", xml).length, 1, "Element Selector with underscore" );
42+
equals( jQuery(".component", xml).length, 1, "Class selector" );
43+
equals( jQuery("[class*=component]", xml).length, 1, "Attribute selector for class" );
4244
equals( jQuery("property[name=prop2]", xml).length, 1, "Attribute selector with name" );
4345
equals( jQuery("[name=prop2]", xml).length, 1, "Attribute selector with name" );
4446
equals( jQuery("#seite1", xml).length, 1, "Attribute selector with ID" );

0 commit comments

Comments
 (0)