Added support for escaping selectors in ID and Classname queries (#143). You need to be sure to double-escape selectors, though, as JavaScript requires it to get the right effect.
This commit is contained in:
parent
af79bb1f25
commit
ae208246fb
5
src/jquery/jquery.js
vendored
5
src/jquery/jquery.js
vendored
|
@ -1345,10 +1345,7 @@ jQuery.extend({
|
||||||
|
|
||||||
// internal only, use is(".class")
|
// internal only, use is(".class")
|
||||||
has: function( t, c ) {
|
has: function( t, c ) {
|
||||||
t = t.className || t;
|
return jQuery.inArray( c, (t.className || t).toString().split(/\s+/) ) > -1;
|
||||||
// escape regex characters
|
|
||||||
c = c.replace(/([\.\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
|
|
||||||
return t && new RegExp("(^|\\s)" + c + "(\\s|$)").test( t );
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -76,7 +76,7 @@ jQuery.extend({
|
||||||
/^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/i,
|
/^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/i,
|
||||||
|
|
||||||
// Match: :even, :last-chlid, #id, .class
|
// Match: :even, :last-chlid, #id, .class
|
||||||
/^([:.#]*)([\w\u0128-\uFFFF*-]+)/i
|
/^([:.#]*)((?:[\w\u0128-\uFFFF*-]|\\.)+)/i
|
||||||
],
|
],
|
||||||
|
|
||||||
token: [
|
token: [
|
||||||
|
@ -205,7 +205,7 @@ jQuery.extend({
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Optomize for the case nodeName#idName
|
// 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);
|
var m = re2.exec(t);
|
||||||
|
|
||||||
// Re-organize the results, so that they're consistent
|
// Re-organize the results, so that they're consistent
|
||||||
|
@ -215,10 +215,12 @@ jQuery.extend({
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, do a traditional filter check for
|
// Otherwise, do a traditional filter check for
|
||||||
// ID, class, and element selectors
|
// ID, class, and element selectors
|
||||||
re2 = /^([#.]?)([\w\u0128-\uFFFF*-]*)/i;
|
re2 = /^([#.]?)((?:[\w\u0128-\uFFFF*-]|\\.)*)/i;
|
||||||
m = re2.exec(t);
|
m = re2.exec(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m[2] = m[2].replace(/\\/g, "");
|
||||||
|
|
||||||
var elem = ret[ret.length-1];
|
var elem = ret[ret.length-1];
|
||||||
|
|
||||||
// Try to do a global search by ID, where we can
|
// Try to do a global search by ID, where we can
|
||||||
|
@ -236,10 +238,6 @@ jQuery.extend({
|
||||||
ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : [];
|
ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : [];
|
||||||
|
|
||||||
} else {
|
} 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
|
// We need to find all descendant elements, it is more
|
||||||
// efficient to use getAll() when we are already further down
|
// efficient to use getAll() when we are already further down
|
||||||
// the tree - we try to recognize that here
|
// the tree - we try to recognize that here
|
||||||
|
@ -257,7 +255,7 @@ jQuery.extend({
|
||||||
// It's faster to filter by class and be done with it
|
// It's faster to filter by class and be done with it
|
||||||
if ( m[1] == "." )
|
if ( m[1] == "." )
|
||||||
r = jQuery.grep( r, function(e) {
|
r = jQuery.grep( r, function(e) {
|
||||||
return rec.test(e.className);
|
return jQuery.className.has(e, m[2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Same with ID filtering
|
// Same with ID filtering
|
||||||
|
@ -330,6 +328,8 @@ jQuery.extend({
|
||||||
if ( jQuery.expr[ m[1] ]._resort )
|
if ( jQuery.expr[ m[1] ]._resort )
|
||||||
m = jQuery.expr[ m[1] ]._resort( m );
|
m = jQuery.expr[ m[1] ]._resort( m );
|
||||||
|
|
||||||
|
m[2] = m[2].replace(/\\/g, "");
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -342,17 +342,8 @@ jQuery.extend({
|
||||||
if ( m[1] == ":" && m[2] == "not" )
|
if ( m[1] == ":" && m[2] == "not" )
|
||||||
r = jQuery.filter(m[3], r, true).r;
|
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
|
// Otherwise, find the expression to execute
|
||||||
} else {
|
else {
|
||||||
var f = jQuery.expr[m[1]];
|
var f = jQuery.expr[m[1]];
|
||||||
if ( typeof f != "string" )
|
if ( typeof f != "string" )
|
||||||
f = jQuery.expr[m[1]][m[2]];
|
f = jQuery.expr[m[1]][m[2]];
|
||||||
|
|
|
@ -22,7 +22,7 @@ test("broken", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("id", function() {
|
test("id", function() {
|
||||||
expect(17);
|
expect(23);
|
||||||
t( "ID Selector", "#body", ["body"] );
|
t( "ID Selector", "#body", ["body"] );
|
||||||
t( "ID Selector w/ Element", "body#body", ["body"] );
|
t( "ID Selector w/ Element", "body#body", ["body"] );
|
||||||
t( "ID Selector w/ Element", "ul#first", [] );
|
t( "ID Selector w/ Element", "ul#first", [] );
|
||||||
|
@ -33,6 +33,13 @@ test("id", function() {
|
||||||
t( "Descendant ID selector using UTF8", "div #台北", ["台北"] );
|
t( "Descendant ID selector using UTF8", "div #台北", ["台北"] );
|
||||||
t( "Child ID selector using UTF8", "form > #台北", ["台北"] );
|
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, child ID present", "#form > #radio1", ["radio1"] ); // bug #267
|
||||||
t( "ID Selector, not an ancestor ID", "#form #first", [] );
|
t( "ID Selector, not an ancestor ID", "#form #first", [] );
|
||||||
t( "ID Selector, not a child ID", "#form > #option1a", [] );
|
t( "ID Selector, not a child ID", "#form > #option1a", [] );
|
||||||
|
@ -48,7 +55,7 @@ test("id", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("class", function() {
|
test("class", function() {
|
||||||
expect(10);
|
expect(16);
|
||||||
t( "Class Selector", ".blog", ["mark","simon"] );
|
t( "Class Selector", ".blog", ["mark","simon"] );
|
||||||
t( "Class Selector", ".blog.link", ["simon"] );
|
t( "Class Selector", ".blog.link", ["simon"] );
|
||||||
t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
|
t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
|
||||||
|
@ -60,6 +67,13 @@ test("class", function() {
|
||||||
t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
|
t( "Class selector using UTF8", ".台北Táiběi, .台北", ["utf8class1","utf8class2"] );
|
||||||
t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
|
t( "Descendant class selector using UTF8", "div .台北Táiběi", ["utf8class1"] );
|
||||||
t( "Child class selector using UTF8", "form > .台北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() {
|
test("multiple", function() {
|
||||||
|
|
Loading…
Reference in a new issue