Skip to content

Commit 22c9c9b

Browse files
committed
Make sure that [name=FOO] searches actually have the specified name (IE includes elements that have the ID, as well).
1 parent 2082770 commit 22c9c9b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/selector.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,14 @@ var Expr = Sizzle.selectors = {
332332
},
333333
NAME: function(match, context, isXML){
334334
if ( typeof context.getElementsByName !== "undefined" ) {
335-
var ret = context.getElementsByName(match[1]);
335+
var ret = [], results = context.getElementsByName(match[1]);
336+
337+
for ( var i = 0, l = results.length; i < l; i++ ) {
338+
if ( results[i].getAttribute("name") === match[1] ) {
339+
ret.push( results[i] );
340+
}
341+
}
342+
336343
return ret.length === 0 ? null : ret;
337344
}
338345
},

test/unit/selector.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ test("class", function() {
144144
});
145145

146146
test("name", function() {
147-
expect(9);
147+
expect(11);
148148

149149
t( "Name selector", "input[name=action]", ["text1"] );
150150
t( "Name selector with single quotes", "input[name='action']", ["text1"] );
@@ -158,6 +158,13 @@ test("name", function() {
158158

159159
isSet( jQuery("#form").find("input[name=action]"), q("text1"), "Name selector within the context of another element" );
160160
isSet( jQuery("#form").find("input[name='foo[bar]']"), q("hidden2"), "Name selector for grouped form element within the context of another element" );
161+
162+
var a = jQuery('<a id="tName1ID" name="tName1">tName1 A</a><a id="tName2ID" name="tName2">tName2 A</a><div id="tName1">tName1 Div</div>').appendTo('#main');
163+
164+
t( "Find elements that have similar IDs", "[name=tName1]", ["tName1ID"] );
165+
t( "Find elements that have similar IDs", "[name=tName2]", ["tName2ID"] );
166+
167+
a.remove();
161168
});
162169

163170

0 commit comments

Comments
 (0)