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
104 changes: 73 additions & 31 deletions dist/sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Released under the MIT license
* http://jquery.org/license
*
* Date: 2015-03-24
* Date: 2015-03-29
*/
(function( window ) {

Expand Down Expand Up @@ -470,9 +470,6 @@ function testContext( context ) {
// Expose support vars for convenience
support = Sizzle.support = {};

// Expose expando so it can be removed if needed (#310)
Sizzle.expando = expando;

/**
* Detects XML nodes
* @param {Element|Object} elem An element or a document
Expand Down Expand Up @@ -1145,11 +1142,12 @@ Expr = Sizzle.selectors = {
} :

function( elem, context, xml ) {
var cache, outerCache, node, diff, nodeIndex, start,
var cache, uniqueCache, outerCache, node, nodeIndex, start,
dir = simple !== forward ? "nextSibling" : "previousSibling",
parent = elem.parentNode,
name = ofType && elem.nodeName.toLowerCase(),
useCache = !xml && !ofType;
useCache = !xml && !ofType,
diff = false;

if ( parent ) {

Expand All @@ -1158,7 +1156,10 @@ Expr = Sizzle.selectors = {
while ( dir ) {
node = elem;
while ( (node = node[ dir ]) ) {
if ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) {
if ( ofType ?
node.nodeName.toLowerCase() === name :
node.nodeType === 1 ) {

return false;
}
}
Expand All @@ -1172,11 +1173,21 @@ Expr = Sizzle.selectors = {

// non-xml :nth-child(...) stores cache data on `parent`
if ( forward && useCache ) {

// Seek `elem` from a previously-cached index
outerCache = parent[ expando ] || (parent[ expando ] = {});
cache = outerCache[ type ] || [];
nodeIndex = cache[0] === dirruns && cache[1];
diff = cache[0] === dirruns && cache[2];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needless blank line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's intentional. The first comment applies broadly, the second only to use of node instead of parent.

// ...in a gzip-friendly way
node = parent;
outerCache = node[ expando ] || (node[ expando ] = {});

// Support: IE <9 only
// Defend against cloned attroperties (jQuery gh-1709)
uniqueCache = outerCache[ node.uniqueID ] ||
(outerCache[ node.uniqueID ] = {});

cache = uniqueCache[ type ] || [];
nodeIndex = cache[ 0 ] === dirruns && cache[ 1 ];
diff = nodeIndex && cache[ 2 ];
node = nodeIndex && parent.childNodes[ nodeIndex ];

while ( (node = ++nodeIndex && node && node[ dir ] ||
Expand All @@ -1186,29 +1197,55 @@ Expr = Sizzle.selectors = {

// When found, cache indexes on `parent` and break
if ( node.nodeType === 1 && ++diff && node === elem ) {
outerCache[ type ] = [ dirruns, nodeIndex, diff ];
uniqueCache[ type ] = [ dirruns, nodeIndex, diff ];
break;
}
}

// Use previously-cached element index if available
} else if ( useCache && (cache = (elem[ expando ] || (elem[ expando ] = {}))[ type ]) && cache[0] === dirruns ) {
diff = cache[1];

// xml :nth-child(...) or :nth-last-child(...) or :nth(-last)?-of-type(...)
} else {
// Use the same loop as above to seek `elem` from the start
while ( (node = ++nodeIndex && node && node[ dir ] ||
(diff = nodeIndex = 0) || start.pop()) ) {
// Use previously-cached element index if available
if ( useCache ) {
// ...in a gzip-friendly way
node = elem;
outerCache = node[ expando ] || (node[ expando ] = {});

if ( ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) && ++diff ) {
// Cache the index of each encountered element
if ( useCache ) {
(node[ expando ] || (node[ expando ] = {}))[ type ] = [ dirruns, diff ];
}
// Support: IE <9 only
// Defend against cloned attroperties (jQuery gh-1709)
uniqueCache = outerCache[ node.uniqueID ] ||
(outerCache[ node.uniqueID ] = {});

if ( node === elem ) {
break;
cache = uniqueCache[ type ] || [];
nodeIndex = cache[ 0 ] === dirruns && cache[ 1 ];
diff = nodeIndex;
}

// xml :nth-child(...)
// or :nth-last-child(...) or :nth(-last)?-of-type(...)
if ( diff === false ) {
// Use the same loop as above to seek `elem` from the start
while ( (node = ++nodeIndex && node && node[ dir ] ||
(diff = nodeIndex = 0) || start.pop()) ) {

if ( ( ofType ?
node.nodeName.toLowerCase() === name :
node.nodeType === 1 ) &&
++diff ) {

// Cache the index of each encountered element
if ( useCache ) {
outerCache = node[ expando ] || (node[ expando ] = {});

// Support: IE <9 only
// Defend against cloned attroperties (jQuery gh-1709)
uniqueCache = outerCache[ node.uniqueID ] ||
(outerCache[ node.uniqueID ] = {});

uniqueCache[ type ] = [ dirruns, diff ];
}

if ( node === elem ) {
break;
}
}
}
}
Expand Down Expand Up @@ -1570,10 +1607,10 @@ function addCombinator( matcher, combinator, base ) {

// Check against all ancestor/preceding elements
function( elem, context, xml ) {
var oldCache, outerCache,
var oldCache, uniqueCache, outerCache,
newCache = [ dirruns, doneName ];

// We can't set arbitrary data on XML nodes, so they don't benefit from dir caching
// We can't set arbitrary data on XML nodes, so they don't benefit from combinator caching
if ( xml ) {
while ( (elem = elem[ dir ]) ) {
if ( elem.nodeType === 1 || checkNonElements ) {
Expand All @@ -1586,14 +1623,19 @@ function addCombinator( matcher, combinator, base ) {
while ( (elem = elem[ dir ]) ) {
if ( elem.nodeType === 1 || checkNonElements ) {
outerCache = elem[ expando ] || (elem[ expando ] = {});
if ( (oldCache = outerCache[ dir ]) &&

// Support: IE <9 only
// Defend against cloned attroperties (jQuery gh-1709)
uniqueCache = outerCache[ elem.uniqueID ] || (outerCache[ elem.uniqueID ] = {});

if ( (oldCache = uniqueCache[ dir ]) &&
oldCache[ 0 ] === dirruns && oldCache[ 1 ] === doneName ) {

// Assign to newCache so results back-propagate to previous elements
return (newCache[ 2 ] = oldCache[ 2 ]);
} else {
// Reuse newcache so results back-propagate to previous elements
outerCache[ dir ] = newCache;
uniqueCache[ dir ] = newCache;

// A match means we're done; a fail means we have to keep checking
if ( (newCache[ 2 ] = matcher( elem, context, xml )) ) {
Expand Down
2 changes: 1 addition & 1 deletion dist/sizzle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/sizzle.min.map

Large diffs are not rendered by default.

102 changes: 72 additions & 30 deletions src/sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,6 @@ function testContext( context ) {
// Expose support vars for convenience
support = Sizzle.support = {};

// Expose expando so it can be removed if needed (#310)
Sizzle.expando = expando;

/**
* Detects XML nodes
* @param {Element|Object} elem An element or a document
Expand Down Expand Up @@ -1145,11 +1142,12 @@ Expr = Sizzle.selectors = {
} :

function( elem, context, xml ) {
var cache, outerCache, node, diff, nodeIndex, start,
var cache, uniqueCache, outerCache, node, nodeIndex, start,
dir = simple !== forward ? "nextSibling" : "previousSibling",
parent = elem.parentNode,
name = ofType && elem.nodeName.toLowerCase(),
useCache = !xml && !ofType;
useCache = !xml && !ofType,
diff = false;

if ( parent ) {

Expand All @@ -1158,7 +1156,10 @@ Expr = Sizzle.selectors = {
while ( dir ) {
node = elem;
while ( (node = node[ dir ]) ) {
if ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) {
if ( ofType ?
node.nodeName.toLowerCase() === name :
node.nodeType === 1 ) {

return false;
}
}
Expand All @@ -1172,11 +1173,21 @@ Expr = Sizzle.selectors = {

// non-xml :nth-child(...) stores cache data on `parent`
if ( forward && useCache ) {

// Seek `elem` from a previously-cached index
outerCache = parent[ expando ] || (parent[ expando ] = {});
cache = outerCache[ type ] || [];
nodeIndex = cache[0] === dirruns && cache[1];
diff = cache[0] === dirruns && cache[2];

// ...in a gzip-friendly way
node = parent;
outerCache = node[ expando ] || (node[ expando ] = {});

// Support: IE <9 only
// Defend against cloned attroperties (jQuery gh-1709)
uniqueCache = outerCache[ node.uniqueID ] ||
(outerCache[ node.uniqueID ] = {});

cache = uniqueCache[ type ] || [];
nodeIndex = cache[ 0 ] === dirruns && cache[ 1 ];
diff = nodeIndex && cache[ 2 ];
node = nodeIndex && parent.childNodes[ nodeIndex ];

while ( (node = ++nodeIndex && node && node[ dir ] ||
Expand All @@ -1186,29 +1197,55 @@ Expr = Sizzle.selectors = {

// When found, cache indexes on `parent` and break
if ( node.nodeType === 1 && ++diff && node === elem ) {
outerCache[ type ] = [ dirruns, nodeIndex, diff ];
uniqueCache[ type ] = [ dirruns, nodeIndex, diff ];
break;
}
}

// Use previously-cached element index if available
} else if ( useCache && (cache = (elem[ expando ] || (elem[ expando ] = {}))[ type ]) && cache[0] === dirruns ) {
diff = cache[1];

// xml :nth-child(...) or :nth-last-child(...) or :nth(-last)?-of-type(...)
} else {
// Use the same loop as above to seek `elem` from the start
while ( (node = ++nodeIndex && node && node[ dir ] ||
(diff = nodeIndex = 0) || start.pop()) ) {
// Use previously-cached element index if available
if ( useCache ) {
// ...in a gzip-friendly way
node = elem;
outerCache = node[ expando ] || (node[ expando ] = {});

if ( ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) && ++diff ) {
// Cache the index of each encountered element
if ( useCache ) {
(node[ expando ] || (node[ expando ] = {}))[ type ] = [ dirruns, diff ];
}
// Support: IE <9 only
// Defend against cloned attroperties (jQuery gh-1709)
uniqueCache = outerCache[ node.uniqueID ] ||
(outerCache[ node.uniqueID ] = {});

if ( node === elem ) {
break;
cache = uniqueCache[ type ] || [];
nodeIndex = cache[ 0 ] === dirruns && cache[ 1 ];
diff = nodeIndex;
}

// xml :nth-child(...)
// or :nth-last-child(...) or :nth(-last)?-of-type(...)
if ( diff === false ) {
// Use the same loop as above to seek `elem` from the start
while ( (node = ++nodeIndex && node && node[ dir ] ||
(diff = nodeIndex = 0) || start.pop()) ) {

if ( ( ofType ?
node.nodeName.toLowerCase() === name :
node.nodeType === 1 ) &&
++diff ) {

// Cache the index of each encountered element
if ( useCache ) {
outerCache = node[ expando ] || (node[ expando ] = {});

// Support: IE <9 only
// Defend against cloned attroperties (jQuery gh-1709)
uniqueCache = outerCache[ node.uniqueID ] ||
(outerCache[ node.uniqueID ] = {});

uniqueCache[ type ] = [ dirruns, diff ];
}

if ( node === elem ) {
break;
}
}
}
}
Expand Down Expand Up @@ -1570,10 +1607,10 @@ function addCombinator( matcher, combinator, base ) {

// Check against all ancestor/preceding elements
function( elem, context, xml ) {
var oldCache, outerCache,
var oldCache, uniqueCache, outerCache,
newCache = [ dirruns, doneName ];

// We can't set arbitrary data on XML nodes, so they don't benefit from dir caching
// We can't set arbitrary data on XML nodes, so they don't benefit from combinator caching
if ( xml ) {
while ( (elem = elem[ dir ]) ) {
if ( elem.nodeType === 1 || checkNonElements ) {
Expand All @@ -1586,14 +1623,19 @@ function addCombinator( matcher, combinator, base ) {
while ( (elem = elem[ dir ]) ) {
if ( elem.nodeType === 1 || checkNonElements ) {
outerCache = elem[ expando ] || (elem[ expando ] = {});
if ( (oldCache = outerCache[ dir ]) &&

// Support: IE <9 only
// Defend against cloned attroperties (jQuery gh-1709)
uniqueCache = outerCache[ elem.uniqueID ] || (outerCache[ elem.uniqueID ] = {});

if ( (oldCache = uniqueCache[ dir ]) &&
oldCache[ 0 ] === dirruns && oldCache[ 1 ] === doneName ) {

// Assign to newCache so results back-propagate to previous elements
return (newCache[ 2 ] = oldCache[ 2 ]);
} else {
// Reuse newcache so results back-propagate to previous elements
outerCache[ dir ] = newCache;
uniqueCache[ dir ] = newCache;

// A match means we're done; a fail means we have to keep checking
if ( (newCache[ 2 ] = matcher( elem, context, xml )) ) {
Expand Down
4 changes: 4 additions & 0 deletions test/data/testingPseudos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// A custom pass-through pseudo to force non-qSA processing
Sizzle.selectors.filters.noQSA = function( elem ) {
return true;
};
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<script type="text/javascript" src="data/testinit.js"></script>
<script type="text/javascript" src="data/testrunner.js"></script>
<script type="text/javascript" src="../dist/sizzle.js"></script>
<script type="text/javascript" src="data/testingPseudos.js"></script>
<script type="text/javascript" src="unit/selector.js"></script>
<script type="text/javascript" src="unit/utilities.js"></script>
<script type="text/javascript" src="unit/extending.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/karma/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function( config ) {
files: [
"external/jquery/jquery.js",
"dist/sizzle.js",
"test/data/testingPseudos.js",

// Base fixtures
{
Expand Down
18 changes: 16 additions & 2 deletions test/unit/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,13 +1188,27 @@ test("context", function() {
});

test("caching", function() {
expect( 2 );
expect( 3 );

var sap = document.getElementById("sap");

Sizzle( ":not(code)", document.getElementById("ap") );
deepEqual( Sizzle( ":not(code)", document.getElementById("foo") ), q("sndp", "en", "yahoo", "sap", "anchor2", "simon"), "Reusing selector with new context" );
deepEqual(
Sizzle( ":not(code)", document.getElementById("foo") ),
q( "sndp", "en", "yahoo", "sap", "anchor2", "simon" ),
"Reusing selector with new context"
);

t( "Deep ancestry caching in post-positional element matcher (jQuery #14657)",
"#qunit-fixture a:lt(3):parent",
[ "simon1", "google", "groups" ] );

sap.className = "original";
Sizzle( "#qunit-fixture:noQSA .original" );
document.getElementById("nothiddendiv").appendChild(
sap.cloneNode( true ) ).className = "clone";
equal( Sizzle( "#qunit-fixture:noQSA .clone [href*='2']" ).length, 1,
"Cloning does not poison caches" );
});

asyncTest( "Iframe dispatch should not affect Sizzle, see jQuery #13936", 1, function() {
Expand Down