Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Forked from gavinblair/nonscary.js
Created Jun 15, 2010
Embed
What would you like to do?
/**
* Try and split a selector into its parts (. # are really the only ones supported)
*/
function splitSelector(selector){
var selectors = [];
selector = selector.split(/(\W)/);
while(selector.length){
if(selector[0].match(/(\.|#)/)){
selectors.push(selector.shift() +''+ selector.shift());
} else if(selector[0].match(/\w/)) {
selectors.push(selector.shift());
} else {
selector.shift();
}
}
return selectors;
}
/**
* Dumbed down version of this code:
* Developed by Robert Nyman, http://www.robertnyman.com
* Code/licensing: http://code.google.com/p/getelementsbyclassname/
* @param className string
*/
var getElementsByClassName = function (className){
if (document.getElementsByClassName) {
getElementsByClassName = function (className) {
var tag = '*';
var elm = document;
var elements = elm.getElementsByClassName(className),
nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
returnElements = [],
current;
for(var i=0, il=elements.length; i<il; i+=1){
current = elements[i];
if(!nodeName || nodeName.test(current.nodeName)) {
returnElements.push(current);
}
}
return returnElements;
};
}
else if (document.evaluate) {
getElementsByClassName = function (className) {
var tag = "*",
elm = document,
classes = className.split(" "),
classesToCheck = "",
xhtmlNamespace = "http://www.w3.org/1999/xhtml",
namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
returnElements = [],
elements,
node;
for(var j=0, jl=classes.length; j<jl; j+=1){
classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
}
try {
elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
}
catch (e) {
elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
}
while ((node = elements.iterateNext())) {
returnElements.push(node);
}
return returnElements;
};
}
else {
getElementsByClassName = function (className) {
var tag = "*",
elm = document,
classes = className.split(" "),
classesToCheck = [],
elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
current,
returnElements = [],
match;
for(var k=0, kl=classes.length; k<kl; k+=1){
classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
}
for(var l=0, ll=elements.length; l<ll; l+=1){
current = elements[l];
match = false;
for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
match = classesToCheck[m].test(current.className);
if (!match) {
break;
}
}
if (match) {
returnElements.push(current);
}
}
return returnElements;
};
}
return getElementsByClassName(className, tag, elm);
};
/**
* Select an element based on a simple selector like:
* .class
* element.class
* #id
* element
* element#id
*/
function $(selector){
var elements = [], //the elements that will be returned
matches = [], //matches for one round of the loop
empty = true, //test to see if we have gone through the loop once
selectors = splitSelector(selector); //split the selector up into parts
//while we still have more selectors to match against
while(selectors.length){
selector = selectors.shift();
//if it starts with a '.' treat it as a class
if(selector.match(/^\..*?/)){
selector = selector.replace('.', '');
if(empty) {
matches = getElementsByClassName(selector);
}else if(elements.length){
for(i in elements){
if(elements[i] && undefined !== elements[i].nodeName){
if(hasClass(elements[i], selector)){
matches.push(elements[i]);
}
}
}
}
}
//if it starts with a '#' treat it as an id
else if( selector.match(/^#.*?/) ){
selector = selector.replace('#', '');
if(empty) {
matches.push(document.getElementById(selector));
} else if(elements.length){
for(i in elements){
if(elements[i] && elements[i].id){
if(elements[i].id === selector){
matches.push(elements[i]);
}
}
}
}
}
//finally treat it as a tag name
else {
if(empty){
matches = document.getElementsByTagName(selector);
} else if(elements.length){
for(i in elements){
if(elements[i] && undefined !== elements[i].nodeName){
if(elements[i].nodeName.toUpperCase() === selector.toUpperCase()){
matches.push(elements[i]);
}
}
}
}
}
//elements becomes what we have matched so far
elements = matches;
//clear out matches for the next loop
matches = [];
//empty is now false because we have looped once
empty = false;
}
return elements;
}
function hasClass (obj, className) {
if (typeof obj == 'undefined' || obj==null || !RegExp) {
return false;
}
var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
if (typeof(obj)=="string") {
return re.test(obj);
}
else if (typeof(obj)=="object" && obj.className) {
return re.test(obj.className);
}
return false;
}
function html(selector, html){
var elements = $(selector);
for(i in elements){
if(typeof(elements[i]) == 'object' && elements[i].nodeName){
elements[i].innerHTML = html;
}
}
}
function css(selector, style){
var elements = $(selector);
for(i in elements){
if(typeof(elements[i]) == 'object' && elements[i].nodeName){
elements[i].style.cssText += ';'+style
}
}
}
//iPhone/iPad/iPod browser animations
function anim(selector, transform, opacity, dur){
css(selector, '-webkit-transition:-webkit-transform'+
',opacity '+(dur||0.5)+'s,'+(dur||0.5)+'s;-webkit-transform:'+
transform+';opacity:'+(1||opacity));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment