Skip to content

Commit

Permalink
Added support for escaping selectors in ID and Classname queries (#143)…
Browse files Browse the repository at this point in the history
…. You need to be sure to double-escape selectors, though, as JavaScript requires it to get the right effect.
  • Loading branch information
jeresig committed Mar 25, 2007
1 parent af79bb1 commit ae20824
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
5 changes: 1 addition & 4 deletions src/jquery/jquery.js
Expand Up @@ -1345,10 +1345,7 @@ jQuery.extend({

// internal only, use is(".class")
has: function( t, c ) {
t = t.className || t;
// escape regex characters
c = c.replace(/([\.\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
return t && new RegExp("(^|\\s)" + c + "(\\s|$)").test( t );
return jQuery.inArray( c, (t.className || t).toString().split(/\s+/) ) > -1;
}
},

Expand Down
27 changes: 9 additions & 18 deletions src/selector/selector.js
Expand Up @@ -76,7 +76,7 @@ jQuery.extend({
/^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/i,

// Match: :even, :last-chlid, #id, .class
/^([:.#]*)([\w\u0128-\uFFFF*-]+)/i
/^([:.#]*)((?:[\w\u0128-\uFFFF*-]|\\.)+)/i
],

token: [
Expand Down Expand Up @@ -205,7 +205,7 @@ jQuery.extend({

} else {
// Optomize for the case nodeName#idName
var re2 = /^(\w+)(#)([\w\u0128-\uFFFF*-]+)/i;
var re2 = /^(\w+)(#)((?:[\w\u0128-\uFFFF*-]|\\.)+)/i;
var m = re2.exec(t);

// Re-organize the results, so that they're consistent
Expand All @@ -215,10 +215,12 @@ jQuery.extend({
} else {
// Otherwise, do a traditional filter check for
// ID, class, and element selectors
re2 = /^([#.]?)([\w\u0128-\uFFFF*-]*)/i;
re2 = /^([#.]?)((?:[\w\u0128-\uFFFF*-]|\\.)*)/i;
m = re2.exec(t);
}

m[2] = m[2].replace(/\\/g, "");

var elem = ret[ret.length-1];

// Try to do a global search by ID, where we can
Expand All @@ -236,10 +238,6 @@ jQuery.extend({
ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : [];

} else {
// Pre-compile a regular expression to handle class searches
if ( m[1] == "." )
var rec = new RegExp("(^|\\s)" + m[2] + "(\\s|$)");

// We need to find all descendant elements, it is more
// efficient to use getAll() when we are already further down
// the tree - we try to recognize that here
Expand All @@ -257,7 +255,7 @@ jQuery.extend({
// It's faster to filter by class and be done with it
if ( m[1] == "." )
r = jQuery.grep( r, function(e) {
return rec.test(e.className);
return jQuery.className.has(e, m[2]);
});

// Same with ID filtering
Expand Down Expand Up @@ -330,6 +328,8 @@ jQuery.extend({
if ( jQuery.expr[ m[1] ]._resort )
m = jQuery.expr[ m[1] ]._resort( m );

m[2] = m[2].replace(/\\/g, "");

return false;
}
});
Expand All @@ -342,17 +342,8 @@ jQuery.extend({
if ( m[1] == ":" && m[2] == "not" )
r = jQuery.filter(m[3], r, true).r;

// Handle classes as a special case (this will help to
// improve the speed, as the regexp will only be compiled once)
else if ( m[1] == "." ) {

var re = new RegExp("(^|\\s)" + m[2] + "(\\s|$)");
r = jQuery.grep( r, function(e){
return re.test(e.className || "");
}, not);

// Otherwise, find the expression to execute
} else {
else {
var f = jQuery.expr[m[1]];
if ( typeof f != "string" )
f = jQuery.expr[m[1]][m[2]];
Expand Down
18 changes: 16 additions & 2 deletions src/selector/selectorTest.js
Expand Up @@ -22,7 +22,7 @@ test("broken", function() {
});

test("id", function() {
expect(17);
expect(23);
t( "ID Selector", "#body", ["body"] );
t( "ID Selector w/ Element", "body#body", ["body"] );
t( "ID Selector w/ Element", "ul#first", [] );
Expand All @@ -33,6 +33,13 @@ test("id", function() {
t( "Descendant ID selector using UTF8", "div #台北", ["台北"] );
t( "Child ID selector using UTF8", "form > #台北", ["台北"] );

t( "Escaped ID", "#foo\\:bar", ["foo:bar"] );
t( "Escaped ID", "#test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
t( "Descendant escaped ID", "div #foo\\:bar", ["foo:bar"] );
t( "Descendant escaped ID", "div #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
t( "Child escaped ID", "form > #foo\\:bar", ["foo:bar"] );
t( "Child escaped ID", "form > #test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );

t( "ID Selector, child ID present", "#form > #radio1", ["radio1"] ); // bug #267
t( "ID Selector, not an ancestor ID", "#form #first", [] );
t( "ID Selector, not a child ID", "#form > #option1a", [] );
Expand All @@ -48,7 +55,7 @@ test("id", function() {
});

test("class", function() {
expect(10);
expect(16);
t( "Class Selector", ".blog", ["mark","simon"] );
t( "Class Selector", ".blog.link", ["simon"] );
t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
Expand All @@ -60,6 +67,13 @@ test("class", function() {
t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
t( "Child class selector using UTF8", "form > .台北Táiběi", ["utf8class1"] );

t( "Escaped Class", ".foo\\:bar", ["foo:bar"] );
t( "Escaped Class", ".test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
t( "Descendant scaped Class", "div .foo\\:bar", ["foo:bar"] );
t( "Descendant scaped Class", "div .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
t( "Child escaped Class", "form > .foo\\:bar", ["foo:bar"] );
t( "Child escaped Class", "form > .test\\.foo\\[5\\]bar", ["test.foo[5]bar"] );
});

test("multiple", function() {
Expand Down

0 comments on commit ae20824

Please sign in to comment.