From 522ea1b37c0dc8cd7e4c049d69a03e6238fc1ccc Mon Sep 17 00:00:00 2001 From: wartmanm <3869625+wartmanm@users.noreply.github.com> Date: Thu, 25 Oct 2018 18:19:23 -0500 Subject: [PATCH] Selector: avoid calls to setAttribute( "id" ) by examining selector --- src/sizzle.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/sizzle.js b/src/sizzle.js index 90254162..2fa349cc 100644 --- a/src/sizzle.js +++ b/src/sizzle.js @@ -132,6 +132,8 @@ var i, rsibling = /[+~]/, + rcomplex = new RegExp(whitespace + "|>"), + // CSS escapes // http://www.w3.org/TR/CSS21/syndata.html#escaped-characters runescape = new RegExp( "\\\\([\\da-f]{1,6}" + whitespace + "?|(" + whitespace + ")|.)", "ig" ), @@ -304,20 +306,26 @@ function Sizzle( selector, context, results, seed ) { // Exclude object elements } else if ( context.nodeName.toLowerCase() !== "object" ) { - // Capture the context ID, setting it first if necessary - if ( (nid = context.getAttribute( "id" )) ) { - nid = nid.replace( rcssescape, fcssescape ); + // The workaround is only necessary if the selector + // contains child or descendant combinators. + if ( !rcomplex.test( selector ) ) { + newSelector = selector; } else { - context.setAttribute( "id", (nid = expando) ); - } + // Capture the context ID, setting it first if necessary + if ( (nid = context.getAttribute( "id" )) ) { + nid = nid.replace( rcssescape, fcssescape ); + } else { + context.setAttribute( "id", (nid = expando) ); + } - // Prefix every selector in the list - groups = tokenize( selector ); - i = groups.length; - while ( i-- ) { - groups[i] = "#" + nid + " " + toSelector( groups[i] ); + // Prefix every selector in the list + groups = tokenize( selector ); + i = groups.length; + while ( i-- ) { + groups[i] = "#" + nid + " " + toSelector( groups[i] ); + } + newSelector = groups.join( "," ); } - newSelector = groups.join( "," ); // Expand context for sibling selectors newContext = rsibling.test( selector ) && testContext( context.parentNode ) ||