Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
Move runTargetTest into utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
timlindvall committed Apr 10, 2013
1 parent d462e59 commit 342aafb
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions js/hopscotch-0.0.5.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,51 +319,54 @@
}
},

/**
* @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
*/
getStepTarget: function(step) {
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.
Expand Down

0 comments on commit 342aafb

Please sign in to comment.