Skip to content

Instantly share code, notes, and snippets.

@rlr
Created March 29, 2010 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlr/348220 to your computer and use it in GitHub Desktop.
Save rlr/348220 to your computer and use it in GitHub Desktop.
/*
* Proof of Concept
* webkit specific library that (eventually) implements the jQuery API ~ish
*/
(function( window, undefined ) {
var webkitQuery = function (selector, context) {
return new WebkitQuery(selector, context);
};
function WebkitQuery(selector, context){
// Handle $(DOMElement)
if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this;
}
// assume context is a dom element for now...
var nodelist = (context || document).querySelectorAll(selector);
for (var i=0, l=nodelist.length; i<l; i++) {
this[i] = nodelist[i];
}
this.length = nodelist.length;
return this;
}
webkitQuery.fn = {
length: 0,
each: function(callback) {
for (var i=0, l=this.length; i<l; i++) {
callback.call(this[i], i, this[i]);
}
return this;
},
bind: function(type, callback) {
var $this = this;
this.each(function(){
if (typeof type === 'object') {
for ( var key in type ) {
$this.bind(key, type[key]);
}
} else {
this.addEventListener(type, callback, false);
}
});
return this;
},
click: function(callback) {
return this.bind('click', callback);
}
};
WebkitQuery.prototype = webkitQuery.fn;
window.webkitQuery = window.$ = webkitQuery;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment