Skip to content

Instantly share code, notes, and snippets.

@cpojer
Created April 9, 2010 19:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpojer/361474 to your computer and use it in GitHub Desktop.
Save cpojer/361474 to your computer and use it in GitHub Desktop.
/*
* Use the Slick Selector Engine with MooTools Core 1.2.4 by @cpojer
*
* Tested with Slick branch "develop":
* http://github.com/mootools/slick/tree/develop
*
* How to use:
* - Download Slick from github and include Slick.Parser.js and Slick.Finder.js
* - Remove Selectors.js
* - Optionally you can remove all the methods mentioned below from your mootools-core build
* - Search for "var Elements" in mootools-core.js and replace in and the "Elements.implement" statement with:
var Elements = new Native({
initialize: function(elements){
if (elements && elements.length) Slick.uniques(elements, this);
return this;
}
});
Elements.prototype = {length: 0};
Elements.implement({
filter: function(filter, bind){
if (!filter) return this;
return new Elements(Array.filter(this, (typeof filter == 'string') ? function(item){
return item.match(filter);
} : filter, bind));
}.protect(),
push: function(){
var length = this.length;
for (var i = 0, l = arguments.length; i < l; i++){
var item = document.id(arguments[i]);
if (item) this[length++] = item;
}
return (this.length = length);
}.protect()
}).implement(Array.prototype);
*/
(function(){
Native.implement([Element, Document], {
getElement: function(expression){
return document.id(Slick.find(this, expression));
},
getElements: function(expression){
return Slick.search(this, expression, new Elements);
}
});
if (window.$$ == null) Window.implement({$$: function(selector){
return Slick.search(this.document, selector, new Elements);
}});
Element.implement({
getPrevious: function(match){
return document.id(Slick.find(this, '!+ ' + (match || '')));
},
getAllPrevious: function(match){
return Slick.search(this, '!~ ' + (match || ''), new Elements);
},
getNext: function(match){
return document.id(Slick.find(this, '~ ' + (match || '')));
},
getAllNext: function(match){
return Slick.search(this, '~ ' + (match || ''), new Elements);
},
getFirst: function(match){
return document.id(Slick.find(this, '> ' + (match || '')));
},
getLast: function(match){
return document.id(Slick.find(this, '!^ ' + (match || '')));
},
getParent: function(match){
return document.id(Slick.find(this, '! ' + (match || '')));
},
getParents: function(match){
return Slick.search(this, '! ' + (match || ''), new Elements);
},
getSiblings: function(match){
return Slick.search(this, '~~ ' + (match || ''), new Elements);
},
getChildren: function(match){
return Slick.search(this, '> ' + (match || ''), new Elements);
},
getElementById: function(id){
return document.id(Slick.find(this, '#' + id));
},
match: function(expression){
return !expression || Slick.match(this, expression);
}
});
var contains = {contains: function(element){
return Slick.contains(this, element);
}};
if (!document.contains) Document.implement(contains);
if (!document.createElement('div').contains) Element.implement(contains);
Element.implement({hasChild: function(element){
return this !== element && this.contains(element);
}});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment