Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,14 @@ Expr = Sizzle.selectors = {
},

"CLASS": function( className ) {
var pattern = classCache[ expando ][ className ];
if ( !pattern ) {
pattern = classCache( className, new RegExp("(^|" + whitespace + ")" + className + "(" + whitespace + "|$)") );
}
return function( elem ) {
return pattern.test( elem.className || (typeof elem.getAttribute !== strundefined && elem.getAttribute("class")) || "" );
};
// Use cache keys that don't collide with prototype properties (jQuery #12606)
var pattern = classCache[ expando ][ className + " " ];

return pattern ||
(pattern = new RegExp( "(^|" + whitespace + ")" + className + "(" + whitespace + "|$)" )) &&
classCache( className + " ", function( elem ) {
return pattern.test( elem.className || (typeof elem.getAttribute !== strundefined && elem.getAttribute("class")) || "" );
});
},

"ATTR": function( name, operator, check ) {
Expand Down Expand Up @@ -1014,7 +1015,8 @@ Sizzle.error = function( msg ) {

function tokenize( selector, parseOnly ) {
var matched, match, tokens, type, soFar, groups, preFilters,
cached = tokenCache[ expando ][ selector ];
// Use cache keys that don't collide with prototype properties (jQuery #12606)
cached = tokenCache[ expando ][ selector + " " ];

if ( cached ) {
return parseOnly ? 0 : cached.slice( 0 );
Expand Down Expand Up @@ -1071,7 +1073,7 @@ function tokenize( selector, parseOnly ) {
soFar ?
Sizzle.error( selector ) :
// Cache the tokens
tokenCache( selector, groups ).slice( 0 );
tokenCache( selector + " ", groups ).slice( 0 );
}

function addCombinator( matcher, combinator, base ) {
Expand Down Expand Up @@ -1395,7 +1397,8 @@ compile = Sizzle.compile = function( selector, group /* Internal Use Only */ ) {
var i,
setMatchers = [],
elementMatchers = [],
cached = compilerCache[ expando ][ selector ];
// Use cache keys that don't collide with prototype properties (jQuery #12606)
cached = compilerCache[ expando ][ selector + " " ];

if ( !cached ) {
// Generate a function of recursive functions that can be used to check each element
Expand All @@ -1413,7 +1416,7 @@ compile = Sizzle.compile = function( selector, group /* Internal Use Only */ ) {
}

// Cache the compiled function
cached = compilerCache( selector, matcherFromGroupMatchers( elementMatchers, setMatchers ) );
cached = compilerCache( selector + " ", matcherFromGroupMatchers( elementMatchers, setMatchers ) );
}
return cached;
};
Expand Down
14 changes: 10 additions & 4 deletions test/unit/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module("selector", { teardown: moduleTeardown });
*/

test("element", function() {
expect( 36 );
expect( 37 );

equal( Sizzle("").length, 0, "Empty selector returns an empty array" );
equal( Sizzle(" ").length, 0, "Empty selector returns an empty array" );
Expand Down Expand Up @@ -145,6 +145,9 @@ test("element", function() {
ok( !!Sizzle("body div div div").length, "No stack or performance problems with large amounts of descendents" );
ok( !!Sizzle("body>div div div").length, "No stack or performance problems with large amounts of descendents" );
html.remove();

q("qunit-fixture")[0].appendChild( document.createElement("toString") ).id = "toString";
t( "Element name matches Object.prototype property", "toString#toString", ["toString"] );
});

test("XML Document Selectors", function() {
Expand Down Expand Up @@ -265,7 +268,7 @@ test("id", function() {
});

test("class", function() {
expect( 24 );
expect( 25 );

t( "Class Selector", ".blog", ["mark","simon"] );
t( "Class Selector", ".GROUPS", ["groups"] );
Expand All @@ -282,8 +285,8 @@ test("class", function() {

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( "Descendant escaped Class", "div .foo\\:bar", ["foo:bar"] );
t( "Descendant escaped 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"] );

Expand All @@ -302,6 +305,9 @@ test("class", function() {
ok( Sizzle.matchesSelector( div.firstChild, ".null div"), "caching system respects DOM changes" );
ok( !Sizzle.matchesSelector( document, ".foo" ), "testing class on document doesn't error" );
ok( !Sizzle.matchesSelector( window, ".foo" ), "testing class on window doesn't error" );

div.lastChild.className += " hasOwnProperty toString";
deepEqual( Sizzle(".e.hasOwnProperty.toString", div), [ div.lastChild ], "Classes match Object.prototype properties" );
});

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