madrobby/zepto — Issue #321: Accept arbitrary objects in $.qsa
Author: pamelafox
Created: 2011-11-19 18:19:46+00:00
State: open

In my experience of porting a jQuery plugin (https://github.com/jquerytools/jquerytools/tree/master/src/dateinput) to Zepto, I discovered that it passed itself into various jQuery functions, like .add(), where itself was simply a JavaScript object. This resulted in a DOM error when Zepto processed those functions, as it tried to do a querySelectorAll on the object and failed. I fixed it by wrapping the querySelectorAll in a try/catch:

function attemptQuerySelectorAll(element, selector) {
try {
return element.querySelectorAll(selector);
} catch(e) {
return selector;
}
}

$.qsa = $$ = function(element, selector){
var found;
if (element === document && idSelectorRE.test(selector)) {
found = element.getElementById(RegExp.$1);
return found && [found] || [];
} else if (classSelectorRE.test(selector)) {
return slice.call(element.getElementsByClassName(RegExp.$1));
} else if (tagSelectorRE.test(selector)) {
return slice.call(element.getElementsByTagName(selector));
} else {
return slice.call(attemptQuerySelectorAll(element, selector));
}
}

It's not clear to me from the jQuery docs whether plugins are supposed to do this or not, but, well, they apparently do, so perhaps Zepto can accomodate.