From 342aafb5869a6d12d62b429024e087a3b545d097 Mon Sep 17 00:00:00 2001 From: Timothy Lindvall Date: Wed, 10 Apr 2013 15:42:03 -0700 Subject: [PATCH] Move runTargetTest into utils. --- js/hopscotch-0.0.5.js | 59 +++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/js/hopscotch-0.0.5.js b/js/hopscotch-0.0.5.js index d988b310..8326aebb 100644 --- a/js/hopscotch-0.0.5.js +++ b/js/hopscotch-0.0.5.js @@ -319,6 +319,35 @@ } }, + /** + * @private + */ + runTargetTest: function(toTest){ + // Check if it's querySelector-eligible. Only accepting IDs and classes, + // because that's the only thing that makes sense. Tag name and pseudo-class + // are just silly. + if (/^[#\.]/.test(toTest)) { + if (document.querySelector) { + return document.querySelector(toTest); + } + if (hasJquery) { + result = jQuery(toTest); + return result.length ? result[0] : null; + } + if (Sizzle) { + result = new Sizzle(toTest); + return result.length ? result[0] : null; + } + if (step.target[0] === '#' && step.target.indexOf(' ') === -1) { + return document.getElementById(toTest.substring(1)); + } + // Can't extract element. Likely IE <=7 and no jQuery/Sizzle. + return null; + } + // Else assume it's a string id. + return document.getElementById(toTest); + }, + /** * @private */ @@ -326,44 +355,18 @@ var result, queriedTarged; - function runTargetTest(toTest){ - // Check if it's querySelector-eligible. Only accepting IDs and classes, - // because that's the only thing that makes sense. Tag name and pseudo-class - // are just silly. - if (/^[#\.]/.test(toTest)) { - if (document.querySelector) { - return document.querySelector(toTest); - } - if (hasJquery) { - result = jQuery(toTest); - return result.length ? result[0] : null; - } - if (Sizzle) { - result = new Sizzle(toTest); - return result.length ? result[0] : null; - } - if (step.target[0] === '#' && step.target.indexOf(' ') === -1) { - return document.getElementById(toTest.substring(1)); - } - // Can't extract element. Likely IE <=7 and no jQuery/Sizzle. - return null; - } - // Else assume it's a string id. - return document.getElementById(toTest); - } - if (!step || !step.target) { return null; } if (typeof step.target === 'string') { //Just one target to test. Check, cache, and return its results. - return step.target = runTargetTest(step.target); + return step.target = utils.runTargetTest(step.target); } else if (Array.isArray(step.target)) { //Multiple items to check. Check each and break on first success. var arrSize = step.target.length, i; for (i = 0; i < arrSize; i++){ - queriedTarget = runTargetTest(step.target[i]); + queriedTarget = utils.runTargetTest(step.target[i]); if (queriedTarget !== null){ break; } } //Cache and return.