Accept arbitrary objects in $.qsa #321

pamelafox opened this Issue Nov 19, 2011 · 4 comments


None yet

4 participants


In my experience of porting a jQuery plugin ( 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 {
  } 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)) {
  } else if (tagSelectorRE.test(selector)) {
  } else {
    return 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.


Okay, it turns out this is more complicated than I originally thought, since the plugins also bind and trigger events on the non-DOM objects - so trigger, add, and remove would also need to be modified (they use addEventListener and dispatchEvent now). I've tried modifying but haven't figured out the right way to do it yet.

So maybe this is too significant of a change for now. I'm working around for now.

mislav commented Nov 26, 2011

I don't even know what's going on, but I think if you construct a Zepto instance with non-DOM objects, you can't really expect DOM methods to work for it.

Also, plugins that bind and trigger their own events, especially on non-DOM objects, are simply badly written, so I don't really have great motivation to make Zepto work with them.

Let us know when you have more thoughts on this.

ollym commented Dec 10, 2011

I was thinking about something like this too but more along the lines of using $( { some: 'object' } ) adding a nice enumerable wrapper.

@mislav mislav was assigned Apr 5, 2012
madrobby commented Apr 5, 2012

In the latest master, code like this works:

$({ foo: 'bar' }).each(function(idx,item){ console.log( })
// prints "bar"
@madrobby madrobby closed this Apr 5, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment