Skip to content

Commit b16da9d

Browse files
committed
Reworked the attribute selection code to be able to select false-y values - and added some tests to verify that they work well against expandos.
1 parent ab74ce7 commit b16da9d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/selector.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,16 @@ var Expr = Sizzle.selectors = {
564564
return match.test( elem.className );
565565
},
566566
ATTR: function(elem, match){
567-
var result = Expr.attrHandle[ match[1] ] ? Expr.attrHandle[ match[1] ]( elem ) : elem[ match[1] ] || elem.getAttribute( match[1] ), value = result + "", type = match[2], check = match[4];
567+
var name = match[1],
568+
result = Expr.attrHandle[ name ] ?
569+
Expr.attrHandle[ name ]( elem ) :
570+
elem[ name ] != null ?
571+
elem[ name ] :
572+
elem.getAttribute( name ),
573+
value = result + "",
574+
type = match[2],
575+
check = match[4];
576+
568577
return result == null ?
569578
type === "!=" :
570579
type === "=" ?
@@ -574,7 +583,7 @@ var Expr = Sizzle.selectors = {
574583
type === "~=" ?
575584
(" " + value + " ").indexOf(check) >= 0 :
576585
!check ?
577-
result :
586+
value && result !== false :
578587
type === "!=" ?
579588
value != check :
580589
type === "^=" ?

test/unit/selector.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ test("child and adjacent", function() {
215215
});
216216

217217
test("attributes", function() {
218-
expect(37);
218+
expect(40);
219219
t( "Attribute Exists", "a[title]", ["google"] );
220220
t( "Attribute Exists", "*[title]", ["google"] );
221221
t( "Attribute Exists", "[title]", ["google"] );
@@ -233,6 +233,13 @@ test("attributes", function() {
233233

234234
t( "for Attribute", "form label[for]", ["label-for"] );
235235
t( "for Attribute in form", "#form [for=action]", ["label-for"] );
236+
237+
jQuery("form input")[0].test = 0;
238+
jQuery("form input")[1].test = 1;
239+
240+
t( "Expando attribute", "form input[test]", ["text1", "text2"] );
241+
t( "Expando attribute value", "form input[test=0]", ["text1"] );
242+
t( "Expando attribute value", "form input[test=1]", ["text2"] );
236243

237244
t( "Attribute containing []", "input[name^='foo[']", ["hidden2"] );
238245
t( "Attribute containing []", "input[name^='foo[bar]']", ["hidden2"] );

0 commit comments

Comments
 (0)