From 22c9c9b9d31933a3615892a3796f5d9d75bac104 Mon Sep 17 00:00:00 2001 From: John Resig Date: Sun, 15 Feb 2009 22:33:19 +0000 Subject: [PATCH] Make sure that [name=FOO] searches actually have the specified name (IE includes elements that have the ID, as well). --- src/selector.js | 9 ++++++++- test/unit/selector.js | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/selector.js b/src/selector.js index f1745e473..4177672eb 100644 --- a/src/selector.js +++ b/src/selector.js @@ -332,7 +332,14 @@ var Expr = Sizzle.selectors = { }, NAME: function(match, context, isXML){ if ( typeof context.getElementsByName !== "undefined" ) { - var ret = context.getElementsByName(match[1]); + var ret = [], results = context.getElementsByName(match[1]); + + for ( var i = 0, l = results.length; i < l; i++ ) { + if ( results[i].getAttribute("name") === match[1] ) { + ret.push( results[i] ); + } + } + return ret.length === 0 ? null : ret; } }, diff --git a/test/unit/selector.js b/test/unit/selector.js index 72511eb66..419116e6d 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -144,7 +144,7 @@ test("class", function() { }); test("name", function() { - expect(9); + expect(11); t( "Name selector", "input[name=action]", ["text1"] ); t( "Name selector with single quotes", "input[name='action']", ["text1"] ); @@ -158,6 +158,13 @@ test("name", function() { isSet( jQuery("#form").find("input[name=action]"), q("text1"), "Name selector within the context of another element" ); isSet( jQuery("#form").find("input[name='foo[bar]']"), q("hidden2"), "Name selector for grouped form element within the context of another element" ); + + var a = jQuery('tName1 AtName2 A
tName1 Div
').appendTo('#main'); + + t( "Find elements that have similar IDs", "[name=tName1]", ["tName1ID"] ); + t( "Find elements that have similar IDs", "[name=tName2]", ["tName2ID"] ); + + a.remove(); });