Skip to content

Instantly share code, notes, and snippets.

@DChinin
Created November 13, 2014 10:31
Show Gist options
  • Save DChinin/98032a9cfba8e8d89b5b to your computer and use it in GitHub Desktop.
Save DChinin/98032a9cfba8e8d89b5b to your computer and use it in GitHub Desktop.
(function(win){
'use strict';
var doc = win.document, simpleRe = /^(#?[\w-]+|\.[\w-.]+)$/, periodRe = /\./g, slice = [].slice;
win.query = function(selector, context){
context = context || doc;
if(simpleRe.test(selector)){
switch(selector.charAt(0)){
case '#':
return context.getElementById(selector.substr(1));
case '.':
return slice.call(context.getElementsByClassName(selector.substr(1).replace(periodRe, ' ')));
default:
return slice.call(context.getElementsByTagName(selector));
}
}
return slice.call(context.querySelectorAll(selector));
};
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment